/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "b_utils/string_utils.h"
#include <iomanip>
#include <sstream>
#include "b_resources/b_constants.h"
#include "filemgmt_libhilog.h"

namespace OHOS::FileManagement::Backup {
bool StringUtils::EndsWith(const std::string& str, const std::string& suffix)
{
    if (suffix.length() > str.length()) {
        return false;
    }
    return str.compare(str.length() - suffix.length(), suffix.length(), suffix) == 0;
}

std::vector<std::string> StringUtils::Split(const std::string& str, const std::string& delimiter)
{
    size_t delimiterLen = delimiter.length();
    if (delimiterLen == 0) {
        return {str};
    }
    std::vector<std::string> result;
    size_t start = 0;
    size_t end = str.find(delimiter);
    while (end != std::string::npos) {
        result.push_back(str.substr(start, end - start));
        start = end + delimiterLen;
        end = str.find(delimiter, start);
    }
    result.push_back(str.substr(start));
    return result;
}

std::string StringUtils::Concat(const std::vector<std::string>& strs, const std::string& connector)
{
    std::string result;
    for (auto &str : strs) {
        result.append(str);
        result.append(connector);
    }
    return result;
}

std::string StringUtils::PathAddDelimiter(const std::string& path)
{
    if (path.empty()) {
        return "/";
    }
    if (path[path.size() - 1] != BConstants::FILE_SEPARATOR_CHAR) {
        return path + BConstants::FILE_SEPARATOR_CHAR;
    }
    return path;
}

std::string StringUtils::GenMappingDir(const std::string& backupDir, const std::string& restoreDir)
{
    return Concat({backupDir, restoreDir}, BConstants::BACKUP_RESTORE_DIR_SEPARATOR);
}

std::pair<std::string, std::string> StringUtils::ParseMappingDir(const std::string& str)
{
    constexpr size_t BACKUP_RESTORE_DIR_SPLIT_SIZE = 3;
    if (!StringUtils::EndsWith(str, BConstants::BACKUP_RESTORE_DIR_SEPARATOR)) {
        return {str, ""};
    }
    std::vector<std::string> splits = Split(str, BConstants::BACKUP_RESTORE_DIR_SEPARATOR);
    if (splits.size() == BACKUP_RESTORE_DIR_SPLIT_SIZE) {
        return {splits[0], splits[1]};
    } else {
        //路径包含separator 等其他异常场景
        HILOGE("path contains seperator or not generated by rule! splits size:%{public}zu", splits.size());
        return {str, ""};
    }
}

std::string StringUtils::GenHashName(const std::string &str)
{
    std::ostringstream strHex;
    strHex << std::hex;
    std::hash<std::string> strHash;
    size_t szHash = strHash(str);
    strHex << std::setfill('0') << std::setw(BConstants::BIG_FILE_NAME_SIZE) << szHash;
    return strHex.str();
}
} // namespace OHOS::FileManagement::Backup